# Copyright (c) 2020 Presto Labs Pte. Ltd.
# Author: xguo, donggu

import requests
from coin2.exchange.product_info import ProductInfoDefault
from coin2.exchange.symbology import SymbologyInfo

exclude_native_symbol_list = ['ETH2.SETH']

def generate():
  mea = 'Spot.Kraken.v1'
  me = 'Spot.Kraken'
  default = ProductInfoDefault()
  symbol_info = SymbologyInfo()
  data = requests.get('https://api.kraken.com/0/public/AssetPairs').json()['result']

  pis = []
  for (native_symbol, info) in data.items():
    if ".d" in native_symbol:
      continue
    if native_symbol in exclude_native_symbol_list:
      continue
    native_base = info['base']
    native_quote = info['quote']
    base, quote = info['wsname'].split('/')
    base = symbol_info.native_to_norm_currency(mea, base)
    quote = symbol_info.native_to_norm_currency(mea, quote)
    symbol = "%s-%s" % (base, quote)

    pair_decimals = info['pair_decimals']
    lot_decimals = info['lot_decimals']
    pair_decimals_format = "{:." + str(pair_decimals) + "f}"
    lot_decinals_format = "{:." + str(lot_decimals) + "f}"

    pi = dict(
        symbol=symbol,
        base=base,
        quote=quote,
        native_symbol=native_symbol,
        native_base=native_base,
        native_quote=native_quote,
        price_ticksize=float(pair_decimals_format.format(0.1**pair_decimals)),
        qty_ticksize=float(lot_decinals_format.format(0.1**lot_decimals)),
        min_qty=float(info['ordermin']),
        maker_fee=info['fees_maker'],
        taker_fee=info['fees'],
        altname=info['altname'],
    )

    pi = {**pi, **default.product_info(me, symbol)}
    pis.append(pi)

  pis = sorted(pis, key=lambda x: x['symbol'])
  return pis


def gen_coin1_constants():
  data = generate()
  data = convert_coin1_constants(data)
  return data


def convert_coin1_constants(data):
  symbol_list = [info['native_symbol'] for info in data]
  quote_currency_list = [info['native_quote'] for info in data]
  base_currency_list = [info['native_base'] for info in data]
  currency_list = base_currency_list + quote_currency_list
  res = {
      'clean_symbol_list': list(set(symbol_list)),
      'quote_currency_list': list(set(quote_currency_list)),
      'currency_list': list(set(currency_list)),
  }
  return res


if __name__ == "__main__":
  import json
  print(json.dumps(generate(), indent=2))
